//
//  MainViewController.m
//  SinaWeiBo123
//
//  Created by wujunjie on 2017/3/8.
//  Copyright © 2017年 wujunjie. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "UIImage+extension.h"
@interface MainViewController ()
@property(nonatomic,strong)UIButton *composeButton;
@end

@implementation MainViewController

+(void)initialize
{
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateSelected];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self addComposeButton];
    [self addTabBarLine];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self addChildViewController];
   
}

-(void)addChildViewController{
    
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    [self createChildViewController:homeVC title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    MessageViewController *messageVC = [[MessageViewController alloc]init];
    [self createChildViewController:messageVC title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    
    UIViewController *composeVC = [[UIViewController alloc]init];
    [self createChildViewController:composeVC title:@"" image:@"" selectedImage:@""];
    
    DiscoverViewController *discoverVC = [[DiscoverViewController alloc]init];
    [self createChildViewController:discoverVC title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    
    ProfileViewController *profileVC = [[ProfileViewController alloc]init];
    [self createChildViewController:profileVC title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
}

-(void)addTabBarLine{
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    self.tabBar.shadowImage = [UIImage createAImageWithSize:CGSizeMake(1, 1) andColor:[UIColor blackColor]];
}


-(void)addComposeButton{
    
    UIButton *composeButton = [[UIButton alloc]init];
    self.composeButton = composeButton;
    [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    composeButton.frame = CGRectInset(self.tabBar.bounds, [UIScreen mainScreen].bounds.size.width / 5 * 2, 3);

    [composeButton addTarget:self action:@selector(compose)
             forControlEvents:UIControlEventTouchUpInside];
    [composeButton sizeToFit];
    [self.tabBar addSubview:self.composeButton];
    
}

-(void)compose
{
    NSLog(@"sssssssssssssssssssssssssssssssssssssssssssssssssss");
}
-(void)createChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName{
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    vc.title = title;
   
    UIImage *image = [UIImage imageNamed:imageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = image;
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = selectedImage;
    [self addChildViewController:nav];
}

@end
